package com.example.controller;

import com.example.constant.RabbitmqConstant;
import com.example.mapper.TicketMapper;
import com.example.pojo.Ticket;
import com.example.service.ThreadService;
import com.example.service.TicketFeignService;
import com.example.service.TicketService;
import com.example.utils.RedisUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/ticket")
public class TicketController{

    @Autowired
    private TicketService ticketService;

    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ThreadService threadService;


    @GetMapping("/selectAll")
    public List<Ticket> getTicket() {
        return ticketService.selectAll();
    }

    @RequestMapping("/insertTicket")
    public boolean insertTicket(Ticket ticket) {
        return ticketService.insertTicket(ticket);
    }

    @GetMapping("/getTicket/{id}")
    public Ticket getTicket(@PathVariable("id") long id){
        System.out.println("2");
        return ticketService.getTicket(id);
    }

    @RequestMapping("/flush/{id}")
    public void flushToRedis(@PathVariable("id") long id){
        int sum = ticketMapper.searchById(id);
        System.out.println(sum);
        if(sum>0){
            String idStr = String.valueOf(id);
            System.out.println(idStr);
            redisUtil.set(idStr,sum);
        }
    }

    @RequestMapping("/updateTicket/{id}")
    public void  updateTicket(@PathVariable("id") long id){
        System.out.println("sum="+redisUtil.get(String.valueOf(id)));
        if((int)redisUtil.get(String.valueOf(id))>0){
            rabbitTemplate.convertAndSend(RabbitmqConstant.TICKET_EXCHANGE,RabbitmqConstant.TICKET_UPDATE_KEY,id);
            threadService.insertOrder(id);
        }else {
            System.out.println("库存不够");
        }
    }

    @RequestMapping("/deleteTicket/{id}")
    public void deleteTicket(@PathVariable("id") long id){
        System.out.println(id);
        rabbitTemplate.convertAndSend(RabbitmqConstant.TICKET_EXCHANGE,RabbitmqConstant.TICKET_DELETE_KEY,id);
    }

    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        List<ServiceInstance> list = discoveryClient.getInstances("provider-ticket");
        for (ServiceInstance service:list){
            System.out.println(service.getHost()+"\t"+
                    service.getPort()+"\t"+
                    service.getUri()+"\t"+
                    service.getServiceId());
        }

        return this.discoveryClient;
    }

}

